﻿var me = function () {
    function sr(x, dz, lx2, i, ox) {
        if (dz[i] === undefined) return repl(x);

        return repl(ox.substring(lx2, dz[i].x1))
            + ox.substring(dz[i].x1, dz[i].x2 + 1)
            + sr(ox.substr(dz[i].x2 + 1), dz, dz[i].x2, i + 1, ox);
    }

    function repl(x) {
        return x.replace(/\n/g, '<br/>').replace(/ /g, '&nbsp;');
    }

    function isc(o, x) {
        if (x.c(o) == x.c(o + 1) == x.c(o + 2) == x.c(o + 3) && x.c(o) == ' ' && (x.c(o - 1) == '\n' || x.c(o - 1) === undefined))
            return true;
        return false;
    }

    function getec(o, x) {
        for (var i = o + 4; i <= x.length; i++) {
            if (x.c(i) == '\n') {
                if (isc(i + 1, x) || x.c(i + 1) == '\n') continue;
                return i - 1;
            }
        }
        return x.length - 1;
    }

    function isb(i, s, c) {
        var d = c == '*' ? '_' : '*';
        if (s.c(i) != c) return false;
        if (notin(s.c(i - 1), [' ', '\n', d]) && i !== 0) return false;
        return (notin(s.c(i + 1), [' ', '\n']));
    }

    function geteb(i, s, c) {
        var d = c == '*' ? '_' : '*';

        for (var j = i + 2; j <= s.length; j++) {
            if (s.c(j) === undefined) continue;
            if (notin(s.c(j + 1), [' ', '\n', d]) && j != s.length - 1) continue;
            if (s.c(j) == c && notin(s.c(j - 1), [' ', '\n'])) return j;
        }
        return -1;
    }

    function replaceAt(s, i, n) {
        var s1 = s.substring(0, i);
        var s2 = s.substring(i + 1);
        return s1 + n + s2;
    }

    function replaceFromTo(s, f, t, n) {
        var s1 = s.substring(0, f);
        var s2 = s.substring(t + 1);
        return s1 + n + s2;
    }

    function notin(c, arr) {
        for (var i in arr) { if (arr[i] == c) return false; }
        return true;
    }

    function htmlEscape(str) {
        return String(str).replace(/&/g, '&amp;').replace(/"/g, '&quot;').replace(/'/g, '&#39;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/ /g, '&nbsp;').replace(/\n/g, '<br/>');
    }

    function ist(x, i, t) {
        if (x.c(i) != '<') return false;
        var c = '';
        if (x.c(i + 1) == '/') c = '/';

        for (var j in t) {
            var n = x.substr(i + 1 + t[j].length + c.length, 1);
            if (x.substr(i + 1, t[j].length + c.length) == c + t[j] && (n == '>' || n == '/' || n == ' ')) return true;
        }
        return false;
    }

    function smartEncode(x) {
        for (var i = 0; i <= x.length; i++) {
            if (x[i] == '<' && !ist(x, i, ['p', 'b', 'i', 'u', 'hr', 'br', 'pre', 'ul', 'li', 'ol']))
                x = replaceAt(x, i, '&lt;');
        }
        return x;
    }


    return {
        init: function () {
            String.prototype.c = function (i) {
                var v = this.charAt(i);
                if (v === '') return undefined;
                return v;
            };
            String.prototype.trim = function () {
                return this.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
            };
        },

        parse: function (x) {
            var roa = [];
            if (x === null || x === undefined) return "";
            for (var o = 0; o <= x.length; o++) {
                if (isc(o, x)) {
                    var ec = getec(o, x);
                    roa.push({ w: o, v: '<pre class="prettyprint" >' + htmlEscape(x.substring(o, ec + 1)) + '</pre>', t: 'c' });
                    x = replaceFromTo(x, o, ec, ' ');
                }
            }

            for (var i = 0; i <= x.length; i++) {
                if (isb(i, x, '*')) {
                    var j = geteb(i, x, '*');
                    if (j == -1) break;
                    roa.push({ w: i, v: '<b>' });
                    roa.push({ w: j, v: '</b>' });
                    i = j;
                }
            }

            for (var y = 0; y <= x.length; y++) {
                if (isb(y, x, '_')) {
                    var b = geteb(y, x, '_');
                    if (b == -1) break;
                    roa.push({ w: y, v: '<i>' });
                    roa.push({ w: b, v: '</i>' });
                    y = b;
                }
            }

            roa = roa.sort(function (g, h) { return g.w - h.w; });

            var dz = [];

            var oo = 0;
            for (var a in roa) {
                x = replaceAt(x, roa[a].w + oo, roa[a].v);
                if (roa[a].t == 'c')
                    dz.push({ x1: roa[a].w + oo, x2: roa[a].v.length + roa[a].w + oo });

                oo += (roa[a].v.length - 1);
            }

            x = sr(x, dz, 0, 0, x);
            x = smartEncode(x);
            return x;
        },

        insertTab: function (o, e) {
            var k = e.keyCode ? e.keyCode : e.charCode ? e.charCode : e.which;
            if (k == 9 && !e.shiftKey && !e.ctrlKey && !e.altKey) {
                var st = o.scrollTop;
                if (o.setSelectionRange) {
                    var sS = o.selectionStart;
                    var sE = o.selectionEnd;
                    o.value = o.value.substring(0, sS) + "    " + o.value.substr(sE);
                    o.setSelectionRange(sS + 4, sS + 4);
                    o.focus();
                }
                else if (o.createTextRange) {
                    document.selection.createRange().text = "    ";
                    e.returnValue = false;
                }
                o.scrollTop = st;
                if (e.preventDefault) e.preventDefault();
                return false;
            }
            return true;
        },

        formatText: function (o, tag) {
            if (o.setSelectionRange) {
                var v = o.value.substring(o.selectionStart, o.selectionEnd);
                if (v.trim().length) {
                    if (tag == 'code') {
                        if (v.substring(0, 4) == '    ')
                            v = v.replace(/\n {4}/g, '\n').substr(4);
                        else
                            v = '    ' + v.replace(/\n/g, '\n    ');
                        o.value = o.value.substring(0, o.selectionStart) + v + o.value.substring(o.selectionEnd, o.value.length);
                    }
                    else o.value = o.value.substring(0, o.selectionStart) + tag + v.trim() + tag + o.value.substring(o.selectionEnd, o.value.length);
                }
            }
            else {
                var x = document.selection.createRange().text;
                if (x.trim().length) {
                    if (tag == 'code') {

                        if (x.substring(0, 4) == '    ')
                            x = x.replace(/\n {4}/g, '\n').substr(4);
                        else
                            x = '    ' + x.replace(/\n/g, '\n    ');
                        document.selection.createRange().text = x;
                    }
                    else {

                        var newText = tag + x + tag;
                        document.selection.createRange().text = newText;
                    }
                }
            }
        }
    };
} ();

